package cn.stylefeng.guns.modular.basic.controller;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.model.params.ArchiveBoxParam;
import cn.stylefeng.guns.modular.basic.service.ArchiveBoxService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.guns.utils.ServiceAssert;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 档案盒信息表控制器
 *
 * @author blesslp
 * @Date 2019-11-17 10:01:29
 */
@Controller
@RequestMapping("/archiveBox")
public class ArchiveBoxController extends BaseController {

    private String PREFIX = "/basic/archiveBox";

    @Autowired
    private ArchiveBoxService archiveBoxService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/archiveBox.html";
    }

    /**
     * 新增页面
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/archiveBox_add.html";
    }

    /**
     * 编辑页面
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/archiveBox_edit.html";
    }

    /**
     * 新增接口
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ArchiveBoxParam archiveBoxParam) {
//        archiveBoxParam.setSsbmid(ShiroKit.getUser().getDeptId().toString());
        this.archiveBoxService.add(archiveBoxParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ArchiveBoxParam archiveBoxParam) {
        this.archiveBoxService.update(archiveBoxParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ArchiveBoxParam archiveBoxParam) {
        Long userId = ShiroKit.getUserNotNull().getId();
        User user = this.userService.getById(userId);

        String oldMd5 = ShiroKit.md5(archiveBoxParam.getPwd(), user.getSalt());

        if (user.getPassword().equals(oldMd5)) {
            //密码比对成功
            this.archiveBoxService.delete(archiveBoxParam);
            return ResponseData.success();
        }
        ServiceAssert.assetTrue(false, "密码错误");
        return ResponseData.success();
    }

    /**
     * 检测是否含有档案
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/check")
    @ResponseBody
    public ResponseData check(ArchiveBoxParam archiveBoxParam) {
        final boolean hasArchiveInfo = this.archiveBoxService.hasArchiveInfo(archiveBoxParam.getId());
        ServiceAssert.assetFalse(hasArchiveInfo, "");
        this.archiveBoxService.delete(archiveBoxParam);
        return ResponseData.success();
    }


    /**
     * 查看详情接口
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ArchiveBoxParam archiveBoxParam) {
        return ResponseData.success(this.archiveBoxService.findBySpec(archiveBoxParam));
    }

    /**
     * 查询列表
     *
     * @author blesslp
     * @Date 2019-11-17
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ArchiveBoxParam archiveBoxParam) {
        return this.archiveBoxService.findPageBySpec(archiveBoxParam);
    }


    @ResponseBody
    @RequestMapping("/tree")
    public List<ZTreeNode> tree(ArchiveBoxParam param) {
        List<ZTreeNode> tree = this.archiveBoxService.tree(param);
        tree.add(ZTreeNode.createParent());
        return tree;
    }

    @RequestMapping("/getMaxCode")
    @ResponseBody
    public ResponseData getMaxCode(@RequestParam(value = "ssdasid", required = true) String ssdasid, @RequestParam(value = "ssdagid", required = true) String ssdagid
            , @RequestParam(value = "ssdagcid", required = true) String ssdagcid) {
        String maxCode = this.archiveBoxService.getMaxCode(ssdasid, ssdagid,ssdagcid);
        return ResponseData.success(StrUtil.isNotBlank(maxCode) ? maxCode : 1);
    }
}


